﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;
using ThreeByte.ActiveDeck.Service;
using System.IO;
using System.Web;

namespace ActiveDeckPublishInstaller
{
    public static class CloudHelper
    {
        private static readonly int BLOCK_SIZE = 32 * 1024;  //32kb
        private static readonly string STORAGE_URI = @"https://activedeck.blob.core.windows.net";
        private static readonly StorageCredentials CREDENTIALS = new StorageCredentialsAccountAndKey("activedeck", "zOPKCq/cIaNQBxplzwoxg2xW47tVkiIksfgXME2uSFIkc0Whh9vQT38ghF9WRJ0IfSPIhUn9oCHOlmSqCwHcxA==");

        public static void StoreDownload(string container, string name, byte[] data, string fileType = null) {

            CloudBlobClient client = new CloudBlobClient(STORAGE_URI, CREDENTIALS);
            CloudBlobContainer blobContainer = new CloudBlobContainer(@"downloads", client);
            //blobContainer.CreateIfNotExist();

            string blobUri = string.Format("downloads/{0}/{1}", container, name);
            CloudBlob b = new CloudBlob(blobUri, client);

            b.UploadByteArray(data);

            if(!(string.IsNullOrEmpty(fileType))) {
                b.Metadata["Type"] = fileType;
            }
            b.SetMetadata();
        }

        public static void StoreResourceBlock(string container, string name, byte[] data, string blockId, string contentHash) {

            CloudBlobClient client = new CloudBlobClient(STORAGE_URI, CREDENTIALS);

            string blobUri = string.Format("downloads/{0}/{1}", container, name);
            CloudBlockBlob bb = new CloudBlockBlob(blobUri, client);
            bb.PutBlock(blockId, new MemoryStream(data), contentHash);
        }

        public static void CommitResourceBlock(string container, string name, List<string> blockIdList, string fileType = null) {
            
            CloudBlobClient client = new CloudBlobClient(STORAGE_URI, CREDENTIALS);

            string blobUri = string.Format("downloads/{0}/{1}", container, name);
            CloudBlockBlob bb = new CloudBlockBlob(blobUri, client);
            bb.PutBlockList(blockIdList);
            if(!(string.IsNullOrEmpty(fileType))) {
                bb.Metadata["Type"] = fileType;
            }
            bb.SetMetadata();
        }

        public static void SegmentAndStoreDownload(string container, string name, byte[] data, string fileType = null) {

            CloudBlobClient client = new CloudBlobClient(STORAGE_URI, CREDENTIALS);
            CloudBlobContainer blobContainer = new CloudBlobContainer(@"downloads", client);
            //blobContainer.CreateIfNotExist();

            ByteBlockGenerator generator = new ByteBlockGenerator(data, BLOCK_SIZE);

            int blocks = 0;
            foreach(ByteBlock bb in generator) {
                blocks++;
                int percent = (blocks * 100) / generator.BlockCount;
                Console.WriteLine("##teamcity[progressMessage 'Uploading {0}%']", percent);
                StoreResourceBlock(container, name, bb.Data, bb.BlockName, bb.ContentHash);
            }

            CommitResourceBlock(container, name, generator.BlockList.BlockIDs, fileType);
        }
    }
}
